package dao.impl;

import dao.LabelDao;
import pojo.Label;
import util.DataUtils;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class LabelDaoImpl implements LabelDao {
    private Connection conn = null;
    private ResultSet rs = null;
    private boolean flag = false;

    /**
     * 查询所有标签
     *
     * @return
     */
    public ArrayList<Label> allLabels() throws SQLException, ClassNotFoundException {
        ArrayList<Label> listLabels = new ArrayList<>();
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select * from label");
//        while (true) {
//            if (!rs.next()) break;
//            Label label = new Label();
//            label.setId(rs.getInt("id"));
//            label.setLabel(rs.getString("label"));
//            listLabels.add(label);
//        }
        rs.last();
        int row = rs.getRow();
        rs.beforeFirst();
        do {
            row--;
            Label label = new Label();
            label = (Label) DataUtils.dataEncapsulation(Class.forName("pojo.Label"), rs);
            listLabels.add(label);
        } while (row > 0);
        JdbcUtils.closeconn(rs, null, conn);
        return listLabels;
    }

    /**
     * 查找标签lid
     *
     * @param label
     * @return
     */
    public Integer findLabelLid(String label) throws SQLException {
        Integer lid = 0;
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select id from label where label='" + label + "'");
        while (true) {
            if (!rs.next()) break;
            lid = rs.getInt("id");
        }
        JdbcUtils.closeconn(rs, null, conn);
        return lid;
    }

    /**
     * 插入自定义标签
     *
     * @param label
     * @return
     */
    public boolean addUserLabel(String label) throws SQLException {
        conn = JdbcUtils.dbconn();
        Integer i = 0;
        String sql = "insert into label values(0,'" + label + "')";
        i = JdbcUtils.addsql(sql);
        if (i > 0) flag = true;
        JdbcUtils.closeconn(null, null, conn);
        return flag;
    }

    /**
     * 过滤重复标签
     *
     * @param
     * @return
     */
    public boolean labelIsExist(String label) throws SQLException {
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select id from label where label='" + label + "'");
        while (true) {
            if (!rs.next()) break;
            flag = true;
        }
        JdbcUtils.closeconn(rs, null, conn);
        return flag;
    }

    /**
     * 根据id查找label名称
     *
     * @param lid
     * @return
     */
    public String findLabelByid(Integer lid) throws SQLException {
        String label = null;
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select label from label where id='" + lid + "'");
        while (true) {
            if (!rs.next()) break;
            label = rs.getString("label");
        }
        JdbcUtils.closeconn(rs, null, conn);
        return label;
    }

    /**
     * 查询到的labels集合
     *
     * @param content
     * @return
     */
    public ArrayList<Label> afterSearchTotalLabel(String content) throws SQLException, ClassNotFoundException {
        ArrayList<Label> searchLabels = new ArrayList<>();
        conn = JdbcUtils.dbconn();
        String sql = "select * from label where label like '%" + content + "%'";
        rs = JdbcUtils.selectsql(sql);
//        while (true) {
//            if (!rs.next()) break;
//            Label label = new Label();
//            label.setLabel(rs.getString("label"));
//            label.setId(rs.getInt("id"));
//            searchLabels.add(label);
//        }
        rs.last();
        int row = rs.getRow();
        rs.beforeFirst();
        do {
            row--;
            Label label = new Label();
            label = (Label) DataUtils.dataEncapsulation(Class.forName("pojo.Label"), rs);
            searchLabels.add(label);
        } while (row > 0);
        return searchLabels;
    }

}
